﻿## ---------------------------
## Script name: 3_draw_figures.R
## Author:         Leo Y. Yang
## Date Created:    2022-06-11
## Email:     LeoYang@ucsd.edu
##
## ---------------------------
## Purpose of script:
##   Draw all the figures
##
## ---------------------------


# Load packages -----------------------------------------------------------
library(tidyverse)
library(haven)

# Setup Working Directory -------------------------------------------------
setwd("/Your/Working/Directory/PSRM_code_data")

# Figure 1 ----------------------------------------------------------------
## Annual News Reports on the Politburo Standing Committee Members
read_csv("data/patron_power_by_year.csv") %>% 
  filter(year <= 2014) %>% 
  mutate(PP = ifelse(Eng_name %in% c("Jiang Zemin", "Zhu Rongji", "Hu Jintao", "Wen Jiabao", "Xi Jinping", "Li Keqiang"), "President and Premier ", "Other PSC Members")) %>% 
  ggplot(aes(year, news_count, group = Eng_name, color = Eng_name, shape = Eng_name, fill = Eng_name)) +
  geom_point(aes(alpha = PP), size = 3) +
  geom_line(aes(alpha = PP), size = 0.8) + 
  scale_alpha_manual(values = c(0.3, 1)) +
  # scale_size_manual(values = c(0.4, 1.3)) +
  scale_shape_manual(values = 0:24) +
  xlab("Year") +
  ylab("News Count") +
  theme_bw() +
  theme(legend.position = "bottom", 
        legend.title = element_blank(),
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

ggsave("image/figure1_annual_news_report.png", width = 10, height = 6.18)

# Figure 2 ----------------------------------------------------------------
## Lowess Plot over News Province Patron’s Power
news.residual <- read_dta("data/final_stata_data_for_regression.dta") %>% 
  lm(nClanInfluence ~ factor(newsprov) + factor(eventprov) + factor(year) + factor(en), data = .) %>% 
  .$residuals %>% 
  as.data.frame() %>% 
  set_names("news_residual") %>% 
  rownames_to_column("id")

read_dta("data/final_stata_data_for_regression.dta") %>% 
  rownames_to_column("id") %>% 
  left_join(news.residual) %>% 
  ggplot(aes(nClanInfluence, news_residual)) +
  geom_jitter(alpha = 0.2) +
  geom_smooth(method = "loess") +
  scale_color_grey(start = 0.2, end = 0.5) + 
  theme_bw() +
  ylab("The Number of Corruption News (Residuals after Controled Fixed-Effect)") +
  xlab("The Power of Factional Patron (Reporting Province)")

ggsave("image/figure2_news_province_power_lowess.png", width = 10, height = 6.18)

# Figure 3 ----------------------------------------------------------------
## Lowess Plot over News-Event Province Patrons’ Power Difference
read_dta("data/final_stata_data_for_regression.dta") %>% 
  rownames_to_column("id") %>% 
  left_join(news.residual) %>% 
  ggplot(aes(diffClanInfluence, news_residual)) +
  geom_jitter(alpha = 0.2) +
  geom_smooth(method = "loess") +
  scale_color_grey(start = 0.2, end = 0.5) + 
  theme_bw() +
  ylab("The Number of Corruption News (Residuals after Controled Fixed-Effect)") +
  xlab("The Power Gap between Factional Patrons (Reporting v.s. Reported)")

ggsave("image/figure3_power_gap_lowess.png", width = 10, height = 6.18)

# Figure A.1 --------------------------------------------------------------
## Monthly News Reports on Presidents and Premiers
read_csv("data/patron_power_by_month.csv") %>% 
  filter(Pinyin_name %in% c("Xijinping", "Likeqiang", "Hujintao", "Wenjiabao", "Jiangzemin", "Zhurongji")) %>% 
  filter(year_month != "2018-04") %>% 
  ggplot(aes(year_month, news_count, group = Eng_name, color = Eng_name, shape = Eng_name)) +
  geom_point(size = 2.5) +
  geom_line() + 
  xlab("Year-Month") +
  ylab("News Count") +
  geom_vline(aes(xintercept = "2002-11"), color = "black", linetype = "dashed") +
  geom_vline(aes(xintercept = "2005-03"), color = "black", linetype = "dashed") +
  geom_vline(aes(xintercept = "2012-11"), color = "black", linetype = "dashed") +
  scale_x_discrete(breaks = function(x){x[c(TRUE, FALSE)]}) +
  theme_bw() +
  theme(legend.position = "bottom", 
        legend.title = element_blank(),
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

ggsave("image/figureA1_pp_news_articles_monthly_count.png", width = 15, height = 9.27)

# Figure A.3 --------------------------------------------------------------
## President’s power and local news on other provinces and the President
province.all.name <- read_csv("data/local_reports_of_province_name_count.csv") 

president <- read_csv("data/patron_power_by_year.csv") %>% 
  filter(year <= 2016) %>% 
  filter(Pinyin_name %in% c("Xijinping", "Likeqiang", "Hujintao", "Wenjiabao", "Jiangzemin", "Zhurongji")) %>% 
  rename(president_name = "news_count") %>% 
  group_by(year) %>%
  mutate(patron_power_all = sum(president_name)) %>%
  ungroup() %>% 
  filter((year > 2012 & Pinyin_name == "Xijinping") | (year <= 2002 & Pinyin_name == "Jiangzemin") | (year > 2002 & year <= 2012 & Pinyin_name == "Hujintao")) %>%
  mutate(relative_patron_power = president_name/patron_power_all)

province.all.name %>% 
  left_join(president, by = c("year" = "year")) %>% 
  filter(year <= 2014) %>% 
  ggplot(aes(x = year, group = 1)) +
  geom_line(aes(y = province_name, colour = "Province Name")) +
  geom_line(aes(y = relative_patron_power * 1900000, colour = "President's Relative Power")) +
  scale_y_continuous(labels = scales::comma,sec.axis = sec_axis(trans = ~./1900000, name = "President's Relative Power")) +
  xlab("Year") +
  ylab("Number of Local News Reports Mentioning Province Name") +
  theme_bw() +
  theme(legend.position = "bottom",
        legend.title = element_blank())

ggsave("image/figureA3top_power_vs_province_name.png", width = 10, height = 6.18)

province.all.name %>% 
  left_join(president, by = c("year" = "year")) %>% 
  filter(year <= 2014) %>% 
  ggplot(aes(x = year, group = 1)) +
  geom_line(aes(y = province_name, colour = "Province Name")) +
  geom_line(aes(y = president_name * 400, colour = "President's Name")) +
  scale_y_continuous(labels = scales::comma,sec.axis = sec_axis(trans = ~./400, name = "Number of Local News Reports Mentioning President's Name")) +
  xlab("Year") +
  ylab("Number of Local News Reports Mentioning Province Name") +
  theme_bw() +
  theme(legend.position = "bottom",
        legend.title = element_blank())

ggsave("image/figureA3bottom_president_name_vs_province_name.png", width = 10, height = 6.18)